

#ifndef CM_YUV420SP_H_
#define CM_YUV420SP_H_

#include <cm/img/cm_img.h>
#include <stddef.h>

/* @brief  计算yuv420sp格式的图像数据一共占多少个字节
 * @param  w和h分别为图像的宽和高
 * @return 字节数
 */
static inline size_t yuv420sp_data_total(unsigned w, unsigned h)
{
	return (w * h + w * h/2);
}

static inline int yuv420sp_y_offset(unsigned x, unsigned y, unsigned w,
		unsigned h)
{
	if (VALID_AREA(x,y,w,h))
		return (int) (y * w + x);
	else
		return -1;
}

static inline int yuv420sp_uv_offset(unsigned x, unsigned y, unsigned w,
		unsigned h)
{
	if (!VALID_AREA(x,y,w,h))
		return -1;
	else
		return (int) ((w * h) + (y >> 1) * (w) + (x - x % 2));

	/* 这里 w*h 是y分量所占的空间, 每一行有w/2个uv分量, 每个uv分量2个字节 */
}


#endif /* CM_YUV420SP_H_ */
